package com.ternnetwork.core.access.controller;

import java.security.Principal;

import com.ternnetwork.core.accesscontroll.dto.UserProfileDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.core.accesscontroll.dto.UserDto;
import com.ternnetwork.core.accesscontroll.dto.UserEditDto;
import com.ternnetwork.core.accesscontroll.dto.UserQueryDto;
import com.ternnetwork.core.accesscontroll.service.UserService;



@Controller
@RequestMapping("/access/user/*")
public class UserController {
	
	 @Autowired
	 private UserService userService;
	
	 @RequestMapping("/getOne")
     public @ResponseBody BaseResponse getOne(Long id) {
     	 return userService.getOne(id);
     }

	 @PreAuthorize("hasAnyAuthority('ACCESS_USER_ADD')")
	 @RequestMapping("/add")
     public @ResponseBody BaseResponse add(String requestJson) throws Exception {
       	 UserDto dto=JsonUtils.jsonToObjectThrowsException(requestJson, UserDto.class);
		 return userService.add(dto);
	 }


	 @PreAuthorize("hasAnyAuthority('ACCESS_USER_UPDATE')")
	 @RequestMapping("/update")
	 //清空用户菜单缓存
	 @CacheEvict(value="userMenu", allEntries=true)
     public @ResponseBody BaseResponse update(String requestJson)  throws Exception{
		 UserEditDto dto=JsonUtils.jsonToObjectThrowsException(requestJson, UserEditDto.class);
   		 return userService.update(dto);
     }

	 @PreAuthorize("hasAnyAuthority('ACCESS_USER_QUERY')")
	 @RequestMapping("/findByPage")
     public @ResponseBody BaseResponse findByPage(@PageableDefault Pageable pageable,UserQueryDto dto) {
      	 return userService.findByPage(pageable, dto);
     }
	@RequestMapping("/findBasicInfoByPage")
	public @ResponseBody BaseResponse findBasicInfoByPage(@PageableDefault Pageable pageable,UserQueryDto dto) {
		return userService.findByPage(pageable, dto);
	}


	 @PreAuthorize("hasAnyAuthority('ACCESS_USER_DELETE')")
	 @RequestMapping("/deleteById")
	 //清空用户菜单缓存
	 @CacheEvict(value="userMenu", allEntries=true)
     public @ResponseBody BaseResponse deleteById(Long id) {
    	 return userService.deleteById(id);
     }


	/**
	 * 按角色代码查找所有用户
	 * @return
	 */
	@RequestMapping("/findAllByRoleName")
	public @ResponseBody BaseResponse findAllByRoleName(String roleNameEn) {
	 	BaseResponse baseResponse=new BaseResponse();
		baseResponse.setSccucess(userService.findByRoleName(roleNameEn));
	 	return baseResponse;
	}

	/**
	 * 查找与当前登录用户角色相同的所有用户
	 * @return
	 */
	@RequestMapping("/findAllTheSameAsLoginUserRole")
	public @ResponseBody BaseResponse findAllTheSameAsLoginUserRole(Principal user) {
		String roleNameEn=userService.findByName(user.getName()).getRoleCodes();
		BaseResponse baseResponse=new BaseResponse();
		baseResponse.setSccucess(userService.findByRoleName(roleNameEn));
		return baseResponse;
	}


	@RequestMapping("/updatePassword")
	public @ResponseBody BaseResponse updatePassword(String orinPassword,String newPassword,String confirmNewPassword,Principal user)  throws Exception{
		return userService.updatePassword(orinPassword,newPassword,confirmNewPassword,user.getName());
	}


	@RequestMapping("/getLoginUser")
	public @ResponseBody BaseResponse getLoginUser(Principal user) {
		BaseResponse baseResponse=new BaseResponse();
		baseResponse.setSccucess(userService.findByName(user.getName()));
		return baseResponse;
	}

	@RequestMapping("/findAllExceptLoginUser")
	public @ResponseBody BaseResponse findAllExceptLoginUser(Principal user) {
		return userService.findAllExceptName(user.getName());
	}

	@RequestMapping("/updateProfile")
	public @ResponseBody BaseResponse updateProfile(String requestJson,Principal user) throws Exception{
		UserProfileDto dto=JsonUtils.jsonToObjectThrowsException(requestJson, UserProfileDto.class);
		return userService.updateProfile(dto,user.getName());
	}


}
